package com.yuexun.dhdoctor.module.contractmanage.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.contractmanage.adapter.RecordAdapter;
import com.yuexun.dhdoctor.module.contractmanage.bean.HospitalizationRecordBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.OutPatientRecordBean;
import com.yuexun.dhdoctor.module.contractmanage.model.impl.ContractManageModel;
import com.yuexun.dhdoctor.module.contractmanage.presenter.IHospitalizationRecordPresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.IOutPatientRecordPresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.HospitalizationRecordPresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.OutPatientRecordPresenter;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.IHospitalizationRecordView;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.IOutPatientRecordView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/10/28.
 */
public class Act_ContractManageRecord extends BaseKJActivity implements IHospitalizationRecordView, IOutPatientRecordView {
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.act_recorddetail_list)
    PullToRefreshList actRecorddetailList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @Bind(R.id.things_agent_tv)
    TextView thingsAgentTv;
    @Bind(R.id.things_agent_img)
    ImageView thingsAgentImg;
    @Bind(R.id.things_do_tv)
    TextView thingsDoTv;
    @Bind(R.id.things_do_img)
    ImageView thingsDoImg;

    private RecordAdapter adapter;

    IOutPatientRecordPresenter presenter;
    IHospitalizationRecordPresenter presenter_hos;

    String healthid, name;

    List<OutPatientRecordBean.RowsBean> outPatientRecordBeans = new ArrayList<>();
    List<HospitalizationRecordBean.RowsBean> hospitalizationRecordBeans = new ArrayList<>();
    List<Object> objects = new ArrayList<Object>();

    private int nowpage = 1;
    private int pagesize = 20;
    private ListView mList;
    private int type =1; //1:门诊，2：住院

    @Override
    public void setRootView() {
        setContentView(R.layout.act_recorddetail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        listViewPreference();
        healthid = getIntent().getExtras().getString("healthid");
        name = getIntent().getExtras().getString("name");
        presenter = new OutPatientRecordPresenter(this, new ContractManageModel());
        presenter_hos = new HospitalizationRecordPresenter(this, new ContractManageModel());
        ApiForGetList_Out();
    }

    //请求详情-门诊
    private void ApiForGetList_Out() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", nowpage + "");
        params.put("pageSize", pagesize + "");
//        params.put("hospId", "41955956");
        params.put("personId", healthid);
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        params.put("endDate", formatter.format(new Date()));
//        params.put("startDate", formatter.format(DateUtil.getDateBefore(new Date(),365)));
        presenter.showOutPatientRecordDate(params);
    }

    //请求详情-住院
    private void ApiForGetList_Hos() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", nowpage + "");
        params.put("pageSize", pagesize + "");
//        params.put("hospId", "41955956");
        params.put("personId", healthid);
        presenter_hos.showHospitalizationRecordDate(params);
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = actRecorddetailList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        actRecorddetailList.setPullLoadEnabled(true);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) actRecorddetailList.getFooterLoadingLayout()).setNoMoreDataText("已经是最新状态");

        adapter = new RecordAdapter(mList, objects, R.layout.recorddetail_item);
        mList.setAdapter(adapter);

        actRecorddetailList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                if (type == 1) {
                    ApiForGetList_Out();
                }
                else {
                    ApiForGetList_Hos();
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                if (type == 1) {
                    ApiForGetList_Out();
                }
                else {
                    ApiForGetList_Hos();
                }
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                if (type == 1) {
                    ApiForGetList_Out();
                }
                else {
                    ApiForGetList_Hos();
                }
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (objects.get(position) instanceof OutPatientRecordBean.RowsBean) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("OutPatientRecordBean", (OutPatientRecordBean.RowsBean) objects.get(position));
                    bundle.putString("name", name);
                    Intent intent = new Intent(aty, Act_RecordOutpatinet.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else if (objects.get(position) instanceof HospitalizationRecordBean.RowsBean) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("HospitalizationRecordBean", (HospitalizationRecordBean.RowsBean) objects.get(position));
                    bundle.putString("name", name);
                    Intent intent = new Intent(aty, Act_RecordHospital.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    @OnClick({R.id.back,R.id.things_agent_ll,R.id.things_do_ll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.things_agent_ll:
                thingsAgentTv.setTextColor(getResources().getColor(R.color.main_text_02));
                thingsAgentImg.setBackgroundColor(getResources().getColor(R.color.main_text_02));
                thingsDoTv.setTextColor(getResources().getColor(R.color.black));
                thingsDoImg.setBackgroundColor(getResources().getColor(R.color.white));
                type = 1;
                nowpage =1;
                ApiForGetList_Out();
                objects.clear();
                adapter.refresh(objects);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                break;
            case R.id.things_do_ll:
                thingsAgentTv.setTextColor(getResources().getColor(R.color.black));
                thingsAgentImg.setBackgroundColor(getResources().getColor(R.color.white));
                thingsDoTv.setTextColor(getResources().getColor(R.color.main_text_02));
                thingsDoImg.setBackgroundColor(getResources().getColor(R.color.main_text_02));
                type = 2;
                nowpage =1;
                ApiForGetList_Hos();
                objects.clear();
                adapter.refresh(objects);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                break;
        }
    }


    protected void setTitle() {
        titleTv.setText("就诊记录");
    }


    @Override
    public void showOutPatientRecordDate(BaseEntity<OutPatientRecordBean> list) {
        if (actRecorddetailList.isPullRefreshEnabled()) {
            actRecorddetailList.onPullDownRefreshComplete();
        }

        if (list.getStatus().equals("200")) {
            if (list.getDatas().size() == 0) {
                actRecorddetailList.setHasMoreData(false);
            } else {
                if (actRecorddetailList.isPullLoadEnabled()) {
                    actRecorddetailList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                objects.clear();
                objects.addAll(list.getDatas().get(0).getRows());
            } else {
                objects.addAll(list.getDatas().get(0).getRows());
            }
            adapter.refresh(objects);
        } else {
            ViewInject.toast(list.getMessage());
        }

        if (objects.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }

    @Override
    public void showHospitalizationRecordDate(BaseEntity<HospitalizationRecordBean> list) {
        if (actRecorddetailList.isPullRefreshEnabled()) {
            actRecorddetailList.onPullDownRefreshComplete();
        }

        if (list.getStatus().equals("200")) {
            if (list.getDatas().size() == 0) {
                actRecorddetailList.setHasMoreData(false);
            } else {
                if (actRecorddetailList.isPullLoadEnabled()) {
                    actRecorddetailList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                objects.clear();
                objects.addAll(list.getDatas().get(0).getRows());
            } else {
                objects.addAll(list.getDatas().get(0).getRows());
            }
            adapter.refresh(objects);
        } else {
            ViewInject.toast(list.getMessage());
        }

        if (objects.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }

}
